package com.xaicode.template.starter;

import com.xaicode.template.starter.properties.StarterProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author Locker
 * @date 2021/8/4 0004 14:40
 */
@Configuration
@EnableConfigurationProperties(StarterProperties.class)
public class StarterAutoConfiguration {

    private final StarterProperties starterProperties;

    @Autowired
    public StarterAutoConfiguration(StarterProperties starterProperties) {
        this.starterProperties = starterProperties;
    }

    /**
     * create class if missing this class and
     * starter.test=true
     */
    @Bean
    @ConditionalOnMissingBean
    @ConditionalOnProperty(prefix = "starter.test", value = "enable", havingValue = "true")
    ExampleService exampleService() {
        return new ExampleService(starterProperties.getName(), starterProperties.getEnable());
    }

}
